package com.eip.zd.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eip.zd.entity.Credit;
import com.eip.zd.entity.Cust;
import com.eip.zd.entity.Standard;
import com.eip.zd.entity.Trade;
import com.eip.zd.exception.CustomException;
import com.eip.zd.mapper.StandardMapper;
import com.eip.zd.mapper.TradeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TradeService {
    @Resource
    private TradeMapper tradeMapper;
    @Resource
    private CreditService creditService;
    @Resource
    private CustService custService;
    @Resource
    private StandardMapper standardMapper;

    public Trade getTradeById(Long id) {
        Trade trade = tradeMapper.getTradeById(id);
        return calc(trade);
    }

    public void add(Trade trade) {
        if (trade.getCar() == null && StrUtil.isNotEmpty(trade.getCarNo())) {
            Standard standard = new Standard();
            standard.setCustId(trade.getCustId());
            standard.setCarNo(trade.getCarNo());
            standardMapper.insert(standard);
            Long id = standard.getId();
            trade.setCar(id);
        }
        trade.setIfSettle(2);
        String format = DateUtil.format(new Date(), "yyMMdd");
        QueryWrapper<Trade> wrapper = new QueryWrapper<>();
        wrapper.likeRight("trade_no", format);
        Integer count = tradeMapper.selectCount(wrapper);
        BigDecimal bigDecimal1 = BigDecimal.valueOf(count);
        BigDecimal bigDecimal2 = new BigDecimal(format + "0000");
        trade.setTradeNo(bigDecimal1.add(bigDecimal2));
        Credit credit = creditService.getCreditByCustId(trade.getCustId());
        Trade calc = calc(trade);
        Cust cust = custService.getCustByCustId(trade.getCustId());
        // 结算类型为信用+未收金额>可用信用额度
        if (cust.getBalance().equals(1)
                && calc.getUnreceive().compareTo(credit.getAvailableCreditQuota()) > 0) {
            throw new CustomException(500, "超过剩余授权额度");
        }
        tradeMapper.insert(trade);
    }

    public void update(Trade trade) {
        tradeMapper.updateById(trade);
    }

    public void remove(Long id) {
        tradeMapper.deleteById(id);
    }

    public List<Trade> getTradeList(String keyword) {
        List<Trade> tradeList = tradeMapper.getTradeList(keyword);
        tradeList.stream().forEach(trade -> {
            calc(trade);
        });
        return tradeList;
    }

    public List<Trade> getTradeListByCustId(String custId) {
        List<Trade> tradeList = tradeMapper.getTradeListByCustId(custId);
        tradeList.stream().forEach(trade -> {
            calc(trade);
        });
        return tradeList;
    }

    public Trade calc(Trade trade) {
        BigDecimal goodWeight = trade.getGoodWeight();
        if (goodWeight == null) {
            goodWeight = BigDecimal.valueOf(0);
        }
        BigDecimal receive = trade.getUnitPrice().multiply(goodWeight);
        BigDecimal unreceive = receive.subtract(trade.getReceived());
        trade.setReceive(receive);
        trade.setUnreceive(unreceive);
        return trade;
    }

}
